package com.xingyun.memorandum;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.RequiresApi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private MyAdapter mAdapter;
    private SharedPreferences sharedPreferences;
    private String currentDate;
    private int currentId;
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        final Button btnOne = (Button) findViewById(R.id.button);
        final ListView list_one = (ListView) findViewById(R.id.list_one);
        LinkedList<Data> mData = new LinkedList<Data>();
        sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
        final Map<String, ?> map = sharedPreferences.getAll();
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            mData.add(new Data(key,val));
            Log.d(key,val);
        }
        // sharedPreferences没有顺序，重新排序一下
        mData.sort(new Comparator<Data>() {
            @Override
            public int compare(Data s, Data t1) {
                return t1.getDate().compareTo(s.getDate());
            }
        });
        mAdapter=new MyAdapter(mData,MainActivity.this);
        list_one.setAdapter(mAdapter);
        list_one.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this,Test.class);
                currentDate=mAdapter.getDate(i);
                currentId=i;
                intent.putExtra("content",mAdapter.getContent(i));
                startActivityForResult(intent,0);
            }
        });
        btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Date date = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                currentDate=formatter.format(date);
                Intent intent = new Intent(MainActivity.this,Test.class);
                currentId=-1;
                intent.putExtra("content","");
                startActivityForResult(intent,0);
            }
        });
        list_one.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
            final AlertDialog alertDialog=new AlertDialog.Builder(MainActivity.this).setTitle("确定删除？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i1) {
                sharedPreferences.edit().remove(mAdapter.getDate(i)).apply();
                mAdapter.remove(i);
                }
            }).setNeutralButton("取消", null).create();
            alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
                    button.setTextColor(Color.GRAY);
                }
            });
            alertDialog.show();
            return true;
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println(data);
        if(data!=null&&!data.getStringExtra("content").isEmpty()){
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString(currentDate,data.getStringExtra("content"));
            edit.apply();
            if(currentId==-1){
                mAdapter.add(new Data(currentDate,data.getStringExtra("content")));
            }else{
                mAdapter.set(currentId,new Data(currentDate,data.getStringExtra("content")));
            }
        }

    }
}